package com.changgou.service.goods.controller;

import com.changgou.common.pojo.PageResult;
import com.changgou.common.pojo.QueryCriteria;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/brand")
@RestController
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping
    public Result findAll() {

        List<Brand> BrandList = brandService.findAll();
        return new Result(true, StatusCode.OK, "品牌查询成功", BrandList);

    }

    @PostMapping
    public Result add(@RequestBody Brand brand) {

        brandService.add(brand);
        return new Result(true, StatusCode.OK, "品牌添加成功");
    }


    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Brand brand, @PathVariable Integer id) {
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "品牌更新成功");
    }

    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Integer id) {
        brandService.delete(id);
        return new Result(true, StatusCode.OK, "品牌删除成功");
    }

    @GetMapping(value = "/{page}/{size}")
    public Result findPage(@PathVariable int page, @PathVariable int size) {
        Page<Brand> pageList = brandService.findPage(page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "品牌列表分页成功", pageResult);
    }

    /*条件和分页查询*/
    @RequestMapping("/search/{page}/{size}")
    public Result findPageAndQuery(  QueryCriteria queryCriteria, @PathVariable int page, @PathVariable int size){


        Page<Brand> pageList= brandService.findPageAndQuery(queryCriteria,page,size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

}
